package com.hmdp.config;

import com.hmdp.Interceptor.Corsinterceptor;
import com.hmdp.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    //@Resource和@Bean差不多，只不过@Resource来自JDK 一个是来自springframework

    //那么用到bean的情况就需要自己定义 一般是没有源码
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Bean
    LoginInterceptor loginInterceptor(){
        return new LoginInterceptor(stringRedisTemplate);
    }
//
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //WebMvcConfigurer.super.addCorsMappings(registry);
//        registry.addMapping("/**").allowedOrigins("http://localhost:8080");
//
//    }

    @Bean
    Corsinterceptor corsinterceptor(){
        return new Corsinterceptor();
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(loginInterceptor()).excludePathPatterns(
                "/shop/**",
                "/voucher/**",
                "/shop-type/**",
                "/upload/**",
                "/blog/hot",
                "/user/code",
                "/user/login"
        );
        registry.addInterceptor(corsinterceptor()).addPathPatterns("/**");
    }


}
